/**
 * @fileoverview
 * This script defines a widget for interacting with image data from BDGP.
 * @author <a href="http://purl.org/net/aliman">Alistair Miles</a>
 * @version $Revision:538 $ on $Date: 2008-08-27 09:08:41 +0100 (Wed, 27 Aug 2008) $ by $Author: aliman $
 * @requires flyui.util
 * @requires flyui.sparql.Service
 * @requires flyui.bdgp.Service
 * @requires YAHOO.util.Connect
 * @requires jQuery
 * For license terms see http://flyui.googlecode.com
 */
 
flyui.namespace("flyui.bdgp");







 
 
 

/*
 * ----------------------------------------------------------------
 *                             WIDGET
 * ----------------------------------------------------------------
 */







flyui.bdgp.BatchWidget = function( service, renderer ) {
    var _context = "flyui.bdgp.BatchWidget";
    try {
        
        flyui.debug("call private constructor", _context);
        this.__init__(service, renderer);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
    
}



 
 



/** @private */
flyui.bdgp.BatchWidget.prototype._controller = null;

/**
 * @private
 * @type flyui.mvcutils.GenericModel2
 */ 
flyui.bdgp.BatchWidget.prototype._model = null;

/**
 * @private
 */
flyui.bdgp.BatchWidget.prototype._renderer = null;

/**
 * @private
 */
flyui.bdgp.BatchWidget.prototype._service = null;










/**
 * @private
 */
flyui.bdgp.BatchWidget.prototype.__init__ = function( service, renderer ) {
    var _context = "flyui.bdgp.BatchWidget.prototype.__init__";
    try {
        
        this._service = service;
        this._renderer = renderer;
        
        flyui.debug("create a model", _context);
        this._model = new flyui.mvcutils.GenericModel2();
        this._model.setDefinition(flyui.bdgp.BatchWidget.modelDefinition);
        
        flyui.debug("create a controller", _context);
        this._controller = new flyui.bdgp.BatchWidget.Controller(this._model, service, this);
        
        flyui.debug("connect the renderer to the model", _context);
        this._renderer.connect(this._model);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};







/**
 * @public
 * @param {Array<String>} querynames
 * @param {Array<flyui.flybase.Gene>} genes
 */
flyui.bdgp.BatchWidget.prototype.findImagesByGeneBatch = function( querynames, genes ) {
    var _context = "flyui.bdgp.BatchWidget.prototype.findGenesByAnyNameBatch";
    try {
        
        flyui.debug("pass ["+querynames+"], ["+genes+"] through to controller", _context);
        this._controller.findImagesByGeneBatch(querynames, genes);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};





flyui.bdgp.BatchWidget.prototype.clear = function() {
    var _context = "flyui.bdgp.BatchWidget.prototype.clear";
    try {
        
        flyui.debug("clear", _context);
        this._controller.clear();
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
}






/*
 * ----------------------------------------------------------------
 *                             CONTROLLER
 * ----------------------------------------------------------------
 */






/**
 * @class
 */
flyui.bdgp.BatchWidget.Controller = function( model, service, controllee ) {
    var _context = "flyui.bdgp.BatchWidget.Controller";
    try {
        
        this._model = null;
        this._service = null;
        this._controllee = null;
        
        flyui.debug("call private constructor", _context);
        this.__init__(model, service, controllee);        
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @private
 */
flyui.bdgp.BatchWidget.Controller.prototype.__init__ = function( model, service, controllee ) {
    var _context = "flyui.bdgp.BatchWidget.Controller.prototype.__init__";
    try {
        
        this._model = model;
        this._service = service;
        this._controllee = controllee;
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @public
 */
flyui.bdgp.BatchWidget.Controller.prototype.findImagesByGeneBatch = function( querynames, genes ) {
    var _context = "flyui.bdgp.BatchWidget.Controller.prototype.findGenesByAnyNameBatch";
    try {
        
        flyui.info("findImagesByGeneBatch: "+querynames+" || "+genes, _context);
        flyui.debug("pass through to private implementation", _context);
        this._findImagesByGeneBatch(querynames, genes, this._findImagesByGeneBatchSuccess(), this._findImagesByGeneBatchFailure());
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @private
 */
flyui.bdgp.BatchWidget.Controller.prototype._findImagesByGeneBatch = function( querynames, genes, success, failure ) {
    var _context = "flyui.bdgp.BatchWidget.Controller.prototype._findImagesByGeneBatch";
    try {
        
        flyui.debug("querynames: "+querynames, _context);
        flyui.debug("genes: "+genes, _context);
        
        flyui.debug("set state pending", _context);
        this._model.set("STATE", "PENDING");
        
        flyui.debug("set model property query", _context);
        this._model.set("QUERY", [querynames, genes]);

        flyui.debug("set result null", _context);
        this._model.set("RESULTS", null);
                
        flyui.debug("set message null", _context);
        this._model.set("MESSAGE", null);
                
        var flybaseids = [];
        for (var i=0;i<genes.length;i++) {
            flybaseids[i] = genes[i].flybaseID;
        }
        flyui.debug("kick off request: "+flybaseids, _context);
        this._service.findImagesByAnyGeneLabelBatch(flybaseids, success, failure);        
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};









/**
 * @private
 */
flyui.bdgp.BatchWidget.Controller.prototype._findImagesByGeneBatchSuccess = function() {
    var _context = "flyui.bdgp.BatchWidget.Controller.prototype._findImagesByGeneBatchSuccess";
    var self = this;
    /**
     */
    return function( results ) {
        try {
            
            flyui.info("request success", _context);
            
            flyui.debug("set model results", _context);
            self._model.set("RESULTS", results);
            
            flyui.debug("set model state", _context);
            self._model.set("STATE", "READY");
            
            var explanation = "Images depict patterns of gene expression during embryogenesis for Drosophila genes represented in non-redundant sets of Drosophila ESTs DGC1 and DGC2. Empty table cells indicate that no images are available from BDGP for that gene/stage. See also <a href='http://dx.doi.org/10.1186/gb-2002-3-12-research0088'>Tomancak et al. 2002</a> and <a href='http://dx.doi.org/10.1186/gb-2007-8-7-r145'>Tomancak et al. 2007</a>.";
            self._model.set("MESSAGE", explanation);
            
        } catch (unexpected) {
            flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
            throw unexpected;    
        }
    };
};







/**
 * @private
 */
flyui.bdgp.BatchWidget.Controller.prototype._findImagesByGeneBatchFailure = function() {
    var _context = "flyui.bdgp.BatchWidget.Controller.prototype._findImagesByGeneBatchFailure";
    var self = this;
    /**
     * @param {ResponseObject} response
     */
    return function( response ) {
        try {
            
            flyui.err("request failed: "+response.status+" "+response.statusText, _context);
            self._model.set("MESSAGE", "there was an error retrieving data from the server, see the logs for more info");
            self._model.set("STATE", "SERVERERROR");
            
        } catch (unexpected) {
            flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
            throw unexpected;    
        }
    };
};







flyui.bdgp.BatchWidget.Controller.prototype.clear = function() {
    var _context = "flyui.bdgp.BatchWidget.Controller.prototype.clear";
    try {
        
        flyui.debug("clear", _context);

        flyui.debug("set state ready", _context);
        this._model.set("STATE", "READY");
        
        flyui.debug("set model property query", _context);
        this._model.set("QUERY", null);

        flyui.debug("set result null", _context);
        this._model.set("RESULTS", null);
                
        flyui.debug("set message null", _context);
        this._model.set("MESSAGE", null);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
}









/*
 * ----------------------------------------------------------------
 *                             MODEL DEFINITION
 * ----------------------------------------------------------------
 */





/** 
 * @type Object
 */
flyui.bdgp.BatchWidget.modelDefinition = {

    properties : [ "STATE", "RESULTS", "MESSAGE", "QUERY" ],
    
    values : {
        "STATE" : [ "READY", "PENDING", "SERVERERROR", "UNEXPECTEDERROR" ]
    },
    
    initialize : function( data ) {
        data["STATE"] = "READY";
        data["RESULTS"] = null;
        data["MESSAGE"] = null;
        data["QUERY"] = null;
    }

};







/*
 * ----------------------------------------------------------------
 *                             DEFAULT RENDERER
 * ----------------------------------------------------------------
 */







flyui.bdgp.BatchWidget.DefaultRenderer = function() {};


/** 
 * @private 
 * @type Element
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._canvas = null;

/**
 * @private 
 * @type Element
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._pendingPane = null;

/**
 * @private 
 * @type Element
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._resultsSummaryPane = null;

/**
 * @private 
 * @type Element
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._resultsPane = null;

/**
 * @private 
 * @type Element
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._messagePane = null;





/**
 * Set the DOM element to which this renderer applies and initialise it.
 * @param {Element} canvas
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype.setCanvas = function( canvas ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype.setCanvas";
    try {
        
        flyui.debug("set canvas", _context);
        this._canvas = $(canvas);
        this._initCanvas();
        
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 * @param {Element} canvas
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._initCanvas = function() {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._initCanvas";
    try {
        
        flyui.debug("begin init canvas", _context);
        
        var canvas = this._canvas;
        canvas.addClass("batchBdgpImageWidget");

        var pp = $("<p class='pendingPane'>pending (please wait, this can take up to 10 seconds) ...</p>").hide();
        canvas.append(pp);
        this._pendingPane = pp;

        var rsp = $("<p class='resultsSummaryPane'>this text should never be displayed</p>").hide();
        canvas.append(rsp);
        this._resultsSummaryPane = rsp;
        
        var rp = $("<div class='resultsPane'></div>").hide();
        canvas.append(rp);
        this._resultsPane = rp;
        
        var mp = $("<p class='messagePane'>this text should never be displayed</p>").hide();
        canvas.append(mp);
        this._messagePane = mp;            

        flyui.debug("init canvas done", _context);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};





/**
 * @public
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype.connect = function( model ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype.connect";
    try {
        
        flyui.debug("connect to model", _context);
        model.subscribeAll(this._onModelChanged, this);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};





/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onModelChanged = function( type, args, self ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onModelChanged";
    try {
        
        var handlers = {
            "STATE":"_onStateChanged",
            "QUERY":"_onQueryChanged",
            "RESULTS":"_onResultsChanged",
            "MESSAGE":"_onMessageChanged"
        };
        var handler = handlers[type];
        flyui.debug("call model changed handler: "+handler, _context);
        self[handler](args[0], args[1], args[2]);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onQueryChanged = function( from, to, get ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onQueryChanged";
    try {
        
        // do nothing, we will access the value later
        flyui.debug("query changed from "+from+" to "+to, _context);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onStateChanged = function( from, to, get ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onStateChanged";
    try {
        
        flyui.debug("state changed from "+from+" to "+to, _context);

        if ( to == "PENDING" ) {
            this._pendingPane.show();
            this._messagePane.hide();
            this._resultsSummaryPane.hide();
            this._resultsPane.hide();
        }
        else if ( to == "READY" ) {
            this._pendingPane.hide();
            this._messagePane.show();
            this._resultsSummaryPane.show();
            this._resultsPane.show();         
        } 
        else if ( to == "SERVERERROR" || to == "UNEXPECTEDERROR" ) {
            this._pendingPane.hide();
            this._messagePane.show();
            this._resultsSummaryPane.hide();
            this._resultsPane.hide();         
        } 
        else {
            // this should never happen
            throw {message:"invalid state: "+to};
        }
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};






/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onResultsChanged = function( from, to, get ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onResultsChanged";
    try {
        
        flyui.debug("results changed to: "+to, _context);
        
        if (to == null) {
            flyui.debug("empty results summary pane", _context);
            this._resultsSummaryPane.empty();
            flyui.debug("empty results pane", _context);
            this._resultsPane.empty();
        } 
        
        else {

            var query = get("QUERY");
            
            flyui.debug("render results summary", _context);
            this._renderResultsSummary(query, to);
    
            flyui.debug("render results", _context);
            this._renderResults(query, to);        
            
        }
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};







/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onMessageChanged = function( from, to, get ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._onMessageChanged";
    try {
        
        flyui.debug("message changed to: "+to, _context);
        
        if (to == null) {
            flyui.debug("empty messsage pane", _context);
            this._messagePane.empty();
        } 
        
        else {

            this._messagePane.html(to);
            
        }
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};









/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._renderResultsSummary = function( query, results ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._renderResultsSummary";
    try {
        
        flyui.debug("render results summary", _context);

        var querynames = query[0];
        var genes = query[1];
        
        var count = 0;
        
        for (genename in results) {
            for (stagename in results[genename]) {
                flyui.debug("counting images for "+genename+" "+stagename, _context);
                count += results[genename][stagename].images.length;               
            }
        }
        
        var content = "found "+count+" images from <a href='http://fruitfly.org'>fruitfly.org</a> ("+flyui.bdgp.provenance+") ...";

        this._resultsSummaryPane.html(content);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};








/**
 * @private
 */
flyui.bdgp.BatchWidget.DefaultRenderer.prototype._renderResults = function( query, results ) {
    var _context = "flyui.bdgp.BatchWidget.DefaultRenderer.prototype._renderResults";
    try {
        flyui.debug("render results", _context);

        var querynames = query[0];
        var genes = query[1];
        
        var stagenames = ["stage1-3", "stage4-6", "stage7-8", "stage9-10", "stage11-12", "stage13-16"];
        
        var content = "<table>";

        content += "<thead><tr><th>gene</th>";
        for (var i=0;i<stagenames.length;i++) {
            content += "<th>"+stagenames[i].replace(/stage(.*)/, "stage $1")+"</th>";
        }                        
        content += "</tr></thead><tbody>";
        
        for (var i=0; i<querynames.length; i++) {
            var n = querynames[i];
            var c = genes[i].annotationSymbols[0];
            var id = genes[i].flybaseID;
            content += "<tr><td><a href='http://www.fruitfly.org/cgi-bin/ex/bquery.pl?qtype=report&amp;find="+c+"&amp;searchfield=CG'>"+n+"</a></td>";
            for (var j=0;j<stagenames.length;j++) {
                var s = stagenames[j];
                var r = results[id];
                flyui.debug("looking for results for gene "+id+" "+r, _context);
                content += "<td>";
                for (var k in r) {
                    flyui.debug("found stage "+k+" in result for gene "+id, _context);
                    if (k == s) {
                        var rs = r[k];
                        flyui.debug("found match to stage "+s, _context);
                        content += "<p class='terms'>";
                        for (var l=0;l<rs.terms.length;l++) {
                            var term = rs.terms[l];
                            content += term + "; ";
                        }
                        content += "</p>";
                        for (var l=0;l<rs.images.length;l++) {
                            var alttext = "thumbnail image depicting expression of "+n+" in "+s+" embryo"
                            
                            var image = rs.images[l];
                            content +=   "<div class=\"result\">";
                            content +=   "<a href=\"" + image.fullImageURL + "\">";
                            content +=   "<img src=\"" + image.thumbnailURL + "\" alt=\""+alttext+"\">";
                            content +=   "</img></a>"; 
                            content +=   "</div>";                                                        
                        }
                    }
                }
                content += "</td>";
            }
            content += "</tr>";
        }

        content += "</tbody></table>";
        
        flyui.debug("results pane content: "+content, _context);
        this._resultsPane.html(content);
        
    } catch (unexpected) {
        flyui.debug("rethrowing "+unexpected.name+", "+unexpected.message, _context);
        throw unexpected;    
    }
};




























